<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.webp" alt="" />
      </div>
      <!-- 表单区域 -->
      <!-- ref用来给元素或者子组件注册信息 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        label-width="0px"
        class="login_form"
        :rules="loginFormRules"
      >
        <el-form-item prop="userName">
          <el-input
            v-model="loginForm.userName"
            prefix-icon="iconfont icon-user"
            @keypress.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input
            v-model="loginForm.passWord"
            prefix-icon="iconfont icon-3702mima"
            @keypress.enter.native="login"
            type="passWord"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="success" @click="zuce">注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      loginForm: {
        userName: "",
        passWord: "",
      },
      loginFormRules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 10, message: "长度在5到10个字符", trigger: "blur" },
        ],
        passWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, max: 12, message: "长度在5到10个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    document.body.removeChild(document.getElementById("Loading")); // 加载页面完后移除加载动画
    // axios
    //   .get("http://localhost:8080/user")
    //   .then((res) => {
    //     console.log("请求成功：", res);
    //   })
    //   .catch((error) => {
    //     console.log(error);
    //   });
    // axios
    //   .get(`/api/v1/test/mock/demo`)
    //   .then((res) => {
    //     console.log("请求成功：", res);
    //   })
    //   .catch((error) => {
    //     console.log(error);
    //   });
  },
  methods: {
    //点击重置按钮进行表单重置
    resetLoginForm() {
      //	resetFields对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.loginFormRef.resetFields(); //通过refs拿到form表单
    },
    login() {
      //去除前后空格
      this.loginForm.userName = this.loginForm.userName.trim();
      this.loginForm.passWord = this.loginForm.passWord.trim();
      //点击登录的时候先调用validate方法验证表单内容是否有误
      this.$refs.loginFormRef.validate(async (item) => {
        // let { data: res } = await this.$http.post("/login", this.loginForm);
        let { data: res } = await axios
          .post("http://localhost:10081/manage/login",this.loginForm)
          // .then((res) => {
          //   console.log("请求成功：", res);
          // })
          // .catch((error) => {
          //   console.log(error);
          // });
        console.log(res);
        if (res.code != 200) {
          return this.$message.error("登录失败");
        }
        this.$message.success("登录成功");
        window.sessionStorage.setItem("token", res.data.token);
        window.sessionStorage.setItem("personInfo", res.data.groupName);
        this.$router.push("/home");
      });
    },
    //注册
    zuce() {
      this.$router.push("/register");
    },
  },
};
</script>
<style lang="less" scoped>
.login_container {
  background-color: #f7f7f7;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>